/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.util;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

import fr.upvm.lem3.vm3d.ui.swing.util.RotatedIcon.Rotate;

/**
 *******************************************************************
 *SwingUtil.java
 *18.02.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
public  class SwingUtil {
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	public  ImageIcon createImageIcon(String path,
	                                           String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
	
	/**
	 * 
	 * @param button the component that the text will be rotate
	 * @param text the text to rotate
	 * @param rotation the rotation direction. 
	 * 			This can one of the following constancy value: 
	 * 			DOWN, (RotatedIcon.Rotate.DOWN)
	 * 			UP,
	 * 			UPSIDE_DOWN,
	 * 			ABOUT_CENTER;
	 * the classes TextIcon, RotateIcon and CompoundIcon (see package util) are the classes implemented to 
	 * achieve the behavior of text rotation
	 */
	public static void rotateComponentText( JButton button, String text, Rotate rotation){
		TextIcon ti = new TextIcon(button, text);
		ImageIcon ii = new ImageIcon("");
		CompoundIcon ci = new CompoundIcon(ii, ti);
		RotatedIcon ri = new RotatedIcon(ci, rotation);
		button.setIcon( ri );
	}
	
	/**
	 * 
	 * @param button
	 * @param text
	 * @param rotation
	 */
	public static void rotateComponentText( JLabel label, String text, Rotate rotation){
		TextIcon ti = new TextIcon(label, text);
		ImageIcon ii = new ImageIcon("");
		CompoundIcon ci = new CompoundIcon(ii, ti);
		RotatedIcon ri = new RotatedIcon(ci, rotation);
		label.setIcon( ri );
	}
}
